package newx.com.control.text
{
	import flash.events.TextEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import newx.com.NxUIComponent;
	import newx.com.control.IText;
	import newx.com.control.Scroll.NxScrollPanel;
	import newx.com.control.Scroll.ScrollPolicy;
	
	public class NxText extends NxUIComponent implements IText
	{
		private var _wordWrap			:Boolean	=	true;
		
		protected var _multiline		:Boolean	=	true;
		
		protected var _textField		:TextField;
		
		private var _scrollPanel		:NxScrollPanel;
		
		private var _hscrollMinSize	:Number;
		
		private var _vscrollMinSize	:Number;
		
		private var _width				:int;
		
		private var _height			:int;
		
		private var _color				:uint		=	0xa1d5fd;
		
		private var _textLeading		:uint		=	0;
		
		protected var _format			:TextFormat = 	new TextFormat();
		
		public function NxText()
		{
			init();
		}
		
		public function get textLeading():uint
		{
			return _textLeading;
		}

		public function set textLeading(value:uint):void
		{
			if(_textLeading != value)
			{
				_textLeading = value;
				
				NxTextFormatManager.setFormatVal(_format,"leading",value);				
				NxTextFormatManager.updataTextFormat(_textField,_format);
			}
		}

		public function get color():uint
		{
			return _color;
		}

		public function set color(value:uint):void
		{
			if(_color != value)
			{
				_color = value;
				_textField.textColor = _color;
			}			
		}

		private function init() : void
		{
			_textField 	= 	new TextField();
			
			_format		=	new TextFormat();
			
			_textField.multiline = true;
			_textField.wordWrap	= _wordWrap;
			_textField.autoSize	= TextFieldAutoSize.LEFT;
			_textField.textColor = _color;
			_textField.selectable = false;
			_textField.mouseWheelEnabled	=	false;
			addChild(_textField);

			_scrollPanel = new NxScrollPanel();
			_scrollPanel.vScrollSkin = "vScrol_1";
			_scrollPanel.hScrollSkin = "vScrol_2";
			_scrollPanel.addView(_textField)
			_scrollPanel.autoScroll = false;
			addChild(_scrollPanel);
			wordWrap 	=	_wordWrap;
			
			_textField.filters = [ NxLabel.glow ];
		}
		
//		public function get contentHeight():int
//		{
//			return _scrollPanel.height;
//		}
//		
//		public function set contentHeight(val:int):void
//		{
//			_scrollPanel.height = val;
//		}
//		
//		public function get contentWidth():int
//		{
//			return _scrollPanel.width;
//		}
//		
//		public function set contentWidth(val:int):void
//		{
//			_scrollPanel.width = val;
//		}
		
		override public function get width():Number
		{
//			return _textField.textWidth;
			return _width;
		}
		
		override public function set width(value:Number):void
		{
			_scrollPanel.width = value;
			
			if(_wordWrap)
			{
				_width = value;
				_textField.width = _width;
			}
			
			updataScrollPanel();
		}
		
		override public function get height():Number
		{
//			return _textField.height;
			return _height;
		}
		
		override public function set height(value:Number):void
		{
			_scrollPanel.height = value;
			
			if(_wordWrap)
			{
				_height = value;
				_textField.height = value;
			}
		}
		
		public function get vScrollSkin():String
		{
			return _scrollPanel.vScrollSkin;
		}
		
		public function set vScrollSkin(val:String):void
		{
			_scrollPanel.vScrollSkin = val;
		}
		
		public function get hScrollSkin():String
		{
			return _scrollPanel.hScrollSkin;
		}
		
		public function set hScrollSkin(val:String):void
		{
			_scrollPanel.hScrollSkin = val;
		}
		
		public function get scrollDistance():uint
		{
			return _scrollPanel.vscrollDistance;
		}
		
		public function set scrollDistance(val:uint):void
		{
			_scrollPanel.vscrollDistance = val;
			_scrollPanel.hscrollDistance = val;
		}
		
		public function get styleSheet():StyleSheet
		{
			return _textField.styleSheet;
		}
		
		public function set styleSheet(val:StyleSheet):void
		{
			_textField.styleSheet = val;
		}
		
		public function get textWidth():uint
		{
			return _textField.textWidth;
		}
		
		public function get textHeight():uint
		{
			return _textField.textHeight;
		}
		
		public function get verticalScrollPolicy():String
		{
			return _scrollPanel.verticalScrollPolicy;
		}
		
		public function set verticalScrollPolicy(val:String):void
		{
			_scrollPanel.verticalScrollPolicy = val;
		}
		
		public function get horizontalScrollPolicy():String
		{
			return _scrollPanel.horizontalScrollPolicy;
		}
		
		public function set horizontalScrollPolicy(val:String):void
		{
			_scrollPanel.horizontalScrollPolicy = val;
		}
		
		public function getTextFormat(beginIndex:int=-1, endIndex:int=-1):TextFormat
		{
			return _textField.getTextFormat(beginIndex, endIndex);
		}
		
		public function setTextFormat(format:TextFormat, beginIndex:int=-1, endIndex:int=-1):void
		{
			NxTextFormatManager.setFormatToOld(format,_format);
			NxTextFormatManager.updataTextFormat(_textField,_format,beginIndex,endIndex);
		}
		
		public function remove():void
		{
			if(parent != null)
			{
				this.removeChild(this);
			}
		}
		
		public function addLinkClickHandel(val:Function):void
		{
			data.HandleProperties("onLinkHandle",val);
			_textField.addEventListener(TextEvent.LINK,onLinkHandle);
		}
		
		public function removeLinkClickHandel():void
		{
			data.DeleteProperties("onLinkHandle");
			_textField.removeEventListener(TextEvent.LINK,onLinkHandle);
		}
		
		private function onLinkHandle(e:TextEvent):void
		{
			var tFunc:Function;
			
			if(data.CheckProperties("onLinkHandle"))
			{
				tFunc	=	data.GetProperties("onLinkHandle") as Function;
				tFunc.call(this,e.text);
				tFunc	=	null;
				//data.DeleteProperties("onLinkHandle");
			}
		}
		
		public function getFilters():Array
		{
			return _textField.filters;
		}
		
		public function setFilters(val:Array):void
		{
			_textField.filters = val;
		}
		
		public function set glowColor( value : int ):void
		{
			var glow:GlowFilter = new GlowFilter(value, 1, 2, 2, 3, BitmapFilterQuality.LOW);
			
			_textField.filters = [glow];
		}
		
		public function get htmlText():String
		{
			return _textField.htmlText;
		}
		
		public function set htmlText(val:String):void
		{
			_textField.htmlText = val;
			
			updataScrollPanel();
		}
		
		public function get text():String
		{
			return _textField.text;
		}
		
		public function set text(val:String):void
		{
			_textField.text = val;
			
			updataScrollPanel();
		}
		
		protected function updataScrollPanel() : void
		{
			_scrollPanel.viewHeight =	_textField.height;
			_scrollPanel.viewWidth =	_textField.width;
//			trace(height, width);
		}
		
		/**
		 *	废弃接口 
		 */
		public function setFilter(val:int = 4):void
		{
			var glow:DropShadowFilter	= 	new DropShadowFilter();
			
			glow.color		= 	0x000000;
			glow.angle 		= 	45;
			glow.alpha 		= 	1;
			glow.blurX 		= 	2;
			glow.blurY 		= 	2;
			glow.distance 	= 	0;
			glow.strength 	= 	val;
			glow.inner 		= 	false;
			glow.knockout 	= 	false;
			glow.quality 	= 	BitmapFilterQuality.LOW;
			
			_textField.filters	=	[glow];
		}
		
		public function get autoScroll():Boolean
		{
			return _scrollPanel.autoScroll;	
		}
		
		public function set autoScroll(value:Boolean):void
		{
			_scrollPanel.autoScroll = value;
		}
		
		public function setScroll(val : int) : void
		{
			_scrollPanel.vscroll = val;
			_scrollPanel.hscroll = val;
		}
		
		public function setVScroll(val : int) : void
		{
			_scrollPanel.vscroll = val;
		}
		
		/**
		 *	废弃接口 
		 */
		public function set htmlStr(val:String):void
		{
			htmlText = val;
		}
		
		/**
		 *	废弃接口 
		 */
		public function get htmlStr():String
		{
			return text;
		}
		
		public function get hscrollMinSize():Number
		{
			return _hscrollMinSize;
		}
		
		public function set hscrollMinSize(val:Number):void
		{
			_hscrollMinSize = val;
			
			_scrollPanel.vscrollMinSize = _hscrollMinSize;
		}
		
		public function get vscrollMinSize():Number
		{
			return _vscrollMinSize;
		}
		
		public function set vscrollMinSize(val:Number):void
		{
			_vscrollMinSize = val;
			
			_scrollPanel.hscrollMinSize = _vscrollMinSize;
		}

		public function get wordWrap():Boolean
		{
			return _wordWrap;
		}

		public function set wordWrap(value:Boolean):void
		{
			_wordWrap = value;
			_textField.wordWrap = _wordWrap;
			
			if(_wordWrap)
			{
				_textField.autoSize		=	TextFieldAutoSize.LEFT;
				horizontalScrollPolicy = ScrollPolicy.OFF;
			}else
			{
				_textField.autoSize		=	TextFieldAutoSize.NONE;
				horizontalScrollPolicy = ScrollPolicy.AUTO;
			}
		}

		public function get multiline():Boolean
		{
			return _multiline;
		}

		public function set multiline(value:Boolean):void
		{
			_multiline = value;
			
			_textField.multiline = value;
			
			if(!_wordWrap || !_multiline)
			{
				_textField.autoSize		=	TextFieldAutoSize.NONE;
				verticalScrollPolicy = ScrollPolicy.OFF;
			}else
			{
				_textField.autoSize		=	TextFieldAutoSize.LEFT;
				verticalScrollPolicy = ScrollPolicy.AUTO;
			}
		}
		
		public function get numLines():int
		{
			return _textField.numLines;
		}
	}
}